package com.feiyu.resource.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feiyu.common.controller.BaseController;
import com.feiyu.common.util.WrapperBuilder;
import com.feiyu.resource.entity.Resource;
import com.feiyu.resource.service.ResourceService;
import org.springblade.core.tool.api.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tang ming
 * @since 2021-11-30
 */
@RestController
@RequestMapping("/resource")
public class ResourceController extends BaseController<ResourceService,Resource> {

    @Autowired
    private ResourceService resourceService;

    @GetMapping("/detail/{id}")
    public R<Resource> detail(@PathVariable("id") long id){
        return R.data(resourceService.getById(id));
    }


    @GetMapping("/list/{id}")
    public R<List<Resource>> list(@PathVariable long id){
        QueryWrapper<Resource> wrapper = new QueryWrapper<>();
        wrapper.eq("ownerId",id);
        return R.data(resourceService.list(wrapper));
    }
    @PostMapping("/list/param")
    public R<IPage<Resource>> listByParam( @RequestBody HashMap<String,Object> param){
        Page<Resource> page=new Page<>();
        page.setCurrent(Long.parseLong(String.valueOf(param.get("current"))));
        page.setSize(Long.parseLong(String.valueOf(param.get("size"))));
        param.remove("current");
        param.remove("size");
        return R.data(resourceService.page(page, WrapperBuilder.getQuery(param)));
        //return R.data(resourceService.list(WrapperBuilder.getQuery(param,Resource.class)));
    }

    @PostMapping("/listChild")
    public R<List<Resource>> listChildren(int id,String code){
        return null;
    }

    @PostMapping("/save")
    public R save(@RequestBody Resource resource){
        if(resource.getId()==null||resource.getId()==0)
            return R.status(resourceService.save(resource));
        return R.status(resourceService.updateById(resource));
    }

    @PostMapping("/delete/{id}")
    public R delete(@PathVariable long id){
        return R.status(resourceService.removeById(id));
    }
}

